<?php

/*
 * This file is part of the Geek-Zoo Projects.
 *
 * @copyright (c) 2010 Geek-Zoo Projects More info http://www.geek-zoo.com
 * @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License
 * @author xuanyan <xuanyan@geek-zoo.com>
 *
 */

class admin_menu_model extends Model
{
    private $data = null;

    function getForum($id)
    {
        $data = $this->getData();
        if (isset($data[$id])) {
            return $data[$id];
        }

        return array();
    }

    function getData()
    {
        if ($this->data === null) {
            $data = $this->getList('ORDER BY view_order ASC');
            foreach ($data as $key => $val) {
                $this->data[$val['id']] = $val;
            }
        }
        $this->data === null && $this->data = array();

        return $this->data;
    }

    function getByParent($parent_id)
    {
        $out = array();
        $data = $this->getData();
        foreach ($data as $key => $val) {
            if ($val['parent_id'] == $parent_id) {
                $out[] = $val;
            }
        }

        return $out;
    }

    function getOption($parent_id = 0)
    {
        $data = $this->getByParent($parent_id);
        $o = new myRecursiveArrayIterator($data);

        return new RecursiveIteratorIterator($o, RecursiveIteratorIterator::SELF_FIRST);
    }
    
}

class myRecursiveArrayIterator extends RecursiveArrayIterator
{
    function hasChildren()
    {
        $c = $this->current();
        $re = _model('admin_menu')->getByParent($c['id']);
        return !empty($re);
    }

    function getChildren()
    {
        $c = $this->current();
        return new myRecursiveArrayIterator(_model('admin_menu')->getByParent($c['id']));
    }
}

?>